home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8609 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: more problems with qsort
  5. Date: 03 Mar 1996 06:16:49 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Mar2231649@qcd.lanl.gov>
  8. References: <177399702S86.JW1675A@american.edu> <4h0j9e$ng5@clarknet.clark.net>
  9.     <4h81m0$avr@solutions.solon.com> <4hba5n$2ga@clarknet.clark.net>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: yom@clark.net's message of 3 Mar 1996 05:19:51 GMT
  14.  
  15. In article <4hba5n$2ga@clarknet.clark.net>
  16. yom@clark.net (yom) writes:
  17. <snip>
  18. y: >
  19. y: >>And your compare function must be defined like this:
  20. y: >
  21. y: >>int compare(char **a,char **b) {return strcmp(*a,*b);}
  22. y: >
  23. y: >No, it really mustn't.  It must be defined like this:
  24. y: >int compare(const void *a, const void *b) {
  25. y: >        return strcmp(*(char **) a, *(char **) b);
  26. y: >}
  27. y: 
  28. y: Well I disagree on this one. Results of 1) defining the compare 
  29. y: function that takes two char** arguments and casting the function call 
  30. y: to qsort and 2) defining the compare function that takes two void* 
  31. y: arguments and casting the arguments to char ** in strcmp call are
  32. y: identical.  Also, I tend to not use "const" because some compilers do
  33. y: not support it.
  34.  
  35. You may disagree, but you will be incorrect. char** and const void*
  36. need not even be of the same size, far less have the same
  37. representation. If the representation/size differs, the cast will do
  38. the conversion. Casting the function type just shuts up the compiler.
  39.  
  40. Also, the current C language defines const: and it requires its use in
  41. some places. Learn to use it, and if you are worried about pre-ANSI
  42. compilers, just ifdef things out.
  43.  
  44. Cheers
  45. Tanmoy
  46. --
  47. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  48. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  49. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  50. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  51. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  52. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  53.